//index.js
//获取应用实例
const app = getApp()
const Util = require('../../utils/util.js');
Page({
  data: {
    images: [],
    imagesNumber:0,
    token:'',
    cateId:''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (e) {
    var that = this;
    that.setData({
      cateId: e.cateId
    });
    wx.getStorage({
      key: 'token',
      success: function (res) {
        that.setData({
          token: res.data
        });
        that.getImagesDetail();
      }
    });
  },
  getImagesDetail:function(){
    Util.showLoading();
    var that = this;
    var token = that.data.token;
    var cateId = that.data.cateId;
    Util.getPhotoDetail({
      "cate_id": cateId,
      "token": token
    }, function (re) {
      if (re.code == 200) {
        console.info(re);
        that.setData({
          images: re.data
        });
      }
      else {
        Util.showMsgDialog(re.msg);
      }
      wx.hideLoading();
    }, function (err) {
      Util.showMsgDialog("操作失败");
    })
  },
  delImages:function(){
    var that = this;
    var token = that.data.token;
    var allImages = that.data.images;
    var allId = '';
    for (var i = 0; i < allImages.length;i++){
      if (allImages[i].url =='../../images/selected.png'){
        allId = allId + allImages[i].id + ',';
      }
    } 
    if (allId){
      wx.showModal({
        title: '提示',
        content: '删除' + that.data.imagesNumber+'张照片？删除后，将无法恢复',
        success: function (res) {
          if (res.confirm) {
            Util.showLoading();
            Util.delPhotoImages({
              "img_id": allId,
              "token": token
            }, function (re) {
              if (re.code == 200) {
                that.getImagesDetail();
                that.setData({
                  imagesNumber: 0
                });
              }
              else {
                Util.showMsgDialog(re.msg);
              }
              wx.hideLoading();
            }, function (err) {
              Util.showMsgDialog("操作失败");
            })
          }
        }
      })
    }    
  },
  cancleImages:function(){
    var imagesNumber = 0;
    var images = this.data.images;
    for(var i=0;i<images.length;i++){
      images[i]['url'] = '../../images/select.png';
    }
    this.setData({
      images: images,
      imagesNumber: imagesNumber
    });
  },
  selectImages:function(e){
    var key = e.currentTarget.dataset.key;
    var images = this.data.images;
    var url = images[key]['url'];
    var imagesNumber = this.data.imagesNumber;
    if (url =='../../images/selected.png'){
      images[key]['url'] = '../../images/select.png';
      imagesNumber = imagesNumber - 1;
    }else{
      images[key]['url'] = '../../images/selected.png';
      imagesNumber = imagesNumber+1;
    }
    this.setData({
      images: images,
      imagesNumber: imagesNumber
    });
  },
  onPullDownRefresh:function(){
    wx.startPullDownRefresh();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 300);
  },
  onShow() {
    app.aData.show = true;
  },
  onHide() {
    app.aData.show = false;
  },
})
